package com.sr.demo.security.login;

import cn.hutool.json.JSONException;
import cn.hutool.json.JSONObject;
import org.springframework.core.log.LogMessage;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

/**
 * 未登录处理类
 * @Author: liushuai
 * @Date: 2021/2/28 11:52
 * @param : * @param null
 * @return :
 **/
@Component
public class NuoExceptionRedirect implements AuthenticationEntryPoint {


    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        PrintWriter writer = null;
        try {
            response.setContentType("application/json;charset=UTF-8");
            writer = response.getWriter();
            JSONObject json = new JSONObject();
            json.set("code","403");
            json.set("message","未认证");
            writer.println(json.toString());
            writer.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (JSONException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }finally {
            writer.close();
        }
    }


}
